package com.listeningframework.boot.commons.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Thread Utils
 *
 * @author listening
 * @version $Id: ThreadUtils.java, v 0.1 2016年2月14日 下午9:09:27 listening Exp $
 */
public class ThreadUtils {
    /**
     * THREAD_POOL
     */
    private static final ThreadPoolExecutor THREAD_POOL = getThreadPool();

    /**
     * Construct ThreadPoolExecutor
     */
    private static final ThreadPoolExecutor getThreadPool() {
        int processors = Runtime.getRuntime().availableProcessors();
        return new ThreadPoolExecutor(processors * 5, processors * 20, 300, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(200, true));
    }

    /**
     * 睡眠秒
     *
     * @param seconds 秒数
     */
    public static void sleep(Long seconds) {
        try {
            Thread.sleep(1000 * seconds);
        } catch (InterruptedException e) {
            throw new RuntimeException("Thread Sleep Error", e);
        }
    }

    /**
     * 线程池执行线程
     *
     * @param runnable 线程任务
     */
    public static void execute(Runnable runnable) {
        THREAD_POOL.execute(runnable);
    }
}
